package szlx;

import entity.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

public class JavaBean {
    public static void main(String[] args) {
        Class clz = Person.class;
        String fullName = clz.getName();
        String simpleName = clz.getName();
        System.out.println("以下是 " + fullName + "类的基本信息");
        System.out.println("---------------------------------------");

        //获取Person类所在的包
        Package pkg = clz.getPackage();
        System.out.println(simpleName + "定义在：" + " pkg.getName()" + "包中");
        System.out.println("-------------------------------------------------------");

        Class superClass = clz.getSuperclass();
        System.out.println(simpleName + "类的超类是：" + superClass.getName());
        System.out.println("------------------------------------------------");

        Class[] interFaces = clz.getInterfaces();
        System.out.println(simpleName + "类所实现的接口：");
        for (Class c : interFaces) {
            System.out.println("\t" + c.getName());
            System.out.println("------------------------------------------------");

            int modifier = clz.getModifiers();
            System.out.println(simpleName + "类的修饰符：");
            if ((modifier & Modifier.PUBLIC) == Modifier.PUBLIC)
                System.out.println("\t访问修饰符是：public");
            else
                System.out.println("\t访问修饰符是：default(package)");
            if ((modifier & Modifier.FINAL) == Modifier.FINAL)
                System.out.println("\t这个类是final的");
            if ((modifier & Modifier.ABSTRACT) == Modifier.ABSTRACT)
                System.out.println("这是一个抽象类");
            if ((modifier & Modifier.INTERFACE) == Modifier.INTERFACE)
                System.out.println("这是一个接口");
            System.out.println("------------------------------------------");

            Constructor[] cons = Person.class.getDeclaredConstructors();
            System.out.println("===========构造方法===========");
            for (Constructor con : cons){
                System.out.println("访问修饰符：");
                int modifier2 = con.getModifiers();
                //判断该构造方法的访问修饰符
                //判断该构造方法的访问修饰符
                if ((modifier & Modifier.PUBLIC)==Modifier.PUBLIC)
                    System.out.println("public");
                else if ((modifier & Modifier.PROTECTED) == Modifier.PROTECTED)
                    System.out.println("protected");
                else if ((modifier & Modifier.PRIVATE)==Modifier.PRIVATE)
                    System.out.println("private");
                else
                    System.out.println("default(package)");

                //获取构造方法的参数列表
                Class[] params = con.getParameterTypes();
                if (params.length == 0){
                    System.out.println("该构造方法没有参数");
                }else{
                    System.out.print("该构造方法的参数列表为:[");
                    for (int i=0;i<params.length;i++) {
                        if (i != 0)
                            System.out.print(",");
                        System.out.print(params[i].getName());
                    }
                    System.out.println("]");
                }
                System.out.println("--------------------------");
            }
        }
    }
}
